%{
AUTHOR: Felipe Arteaga
-------------------------------------------------------------------------
PROJECT: Warnings
-------------------------------------------------------------------------
DESCRIPTION:
=========================================================================
%}


clearvars -except projectDir projectDirData fromMainWarningsPaper
clc;close all;fclose('all');feature('DefaultCharacterSet','UTF-8');

if(not(exist('projectDir','var')==1&&exist('projectDirData','var')==1&&exist('fromMainWarningsPaper','var')==1&&fromMainWarningsPaper))
    pcName=char(java.lang.System.getProperty('user.name'));
    if(strcmp(pcName,'felipe'))
        % PC Felipe
        myDir='/Users/felipe/Dropbox/';
        projectDir=[myDir,'git/warnings/'];
        projectDirData=[myDir,'projects/warnings/'];
        addpath(genpath([myDir,'/myMatlabFunctions/']));
    end
end
compileLatexTable=false;


dirPlots=[projectDir,'/paper/figuresCL/RDs/'];
dirPlotsR=[projectDir,'/paper/figuresCL/RDs_multBandwidth/'];
dirTable=[projectDir,'/paper/tablesCL/'];

dirData=[projectDirData,'/data/chile/'];


%% Load data:
for anho=2018:2020 %0: pooled
if(anho==0)
    anhoStr='';
else
    anhoStr=sprintf('%i',anho);
end


if(anho==0)
    
    d1=load([dirData,'2018/inputRD'],'dataRD');
    d2=load([dirData,'2019/inputRD'],'dataRD');
    d3=load([dirData,'2020/inputRD'],'dataRD');
    % Variables that are not available for 2020 yet:
    fillWithNan={'enrolledInAssigned','enrolledInAddedIniEnd','enrolledInAddedNoWaitlistIniEnd'};
    %fillWithNan={'assignedToPref','acceptOffer','declineOffer','preferenciaAsign_1','placedInAnyPreferenceIniEnd','placedInAddedIniEnd','placedInAddedNoWaitlistIniEnd','placedInOldIniEnd','enrolledInAssigned','enrolledInAddedIniEnd','enrolledInAddedNoWaitlistIniEnd'};
    for v=1:length(fillWithNan)
        d3.dataRD.(fillWithNan{v})=nan(height(d3.dataRD),1);
    end
    
    
    % Universe that is comparable between 2018-2020
    entryGrades=[-1 0 1 7 9];
    sirven1=ismember(d1.dataRD.grade,entryGrades)&not(d1.dataRD.cod_reg==13);
    sirven2=ismember(d2.dataRD.grade,entryGrades)&not(d2.dataRD.cod_reg==13);
    sirven3=ismember(d3.dataRD.grade,entryGrades)&not(d3.dataRD.cod_reg==13);
    
    d1.dataRD.comparable=sirven1;
    d2.dataRD.comparable=sirven2;
    d3.dataRD.comparable=sirven3;
    
    d1.dataRD.anho=2018*ones(height(d1.dataRD),1);
    d2.dataRD.anho=2019*ones(height(d2.dataRD),1);
    d3.dataRD.anho=2020*ones(height(d3.dataRD),1);
    
    % Keep vars in common
    
    incommon=intersect(intersect(d1.dataRD.Properties.VariableNames,d2.dataRD.Properties.VariableNames),d3.dataRD.Properties.VariableNames);
    incommon=incommon(not(strcmp(incommon,'mrun')));
    dataRD=[d1.dataRD(:,incommon);d2.dataRD(:,incommon);d3.dataRD(:,incommon)];
    
    
    
else
    load([dirData,anhoStr,'/inputRD'],'dataRD')
end

dataRD.rural=dataRD.newMarket<0;

% As asignment at .3 is control, just move (if any) in .3 to epsilon to the
% left:
dataRD.riskPopup(dataRD.riskPopup==.3)=.29999999;

% Avoid mass points at extremes:
restrAll=dataRD.pobPopup==1&dataRD.riskPopup>.01&dataRD.riskPopup<.99;%dataRD.mandatoryToAdd==0;

dataRD.Properties.VariableDescriptions{'riskPopup'}='Predicted placement risk first attempt';
dataRD.Properties.VariableDescriptions{'treatedPopup'}='Treated pop-up in first attempt';

dataRD.Properties.VariableDescriptions{'totalPopups'}='Total Pop-ups';




%% First stage
% This is a "generalized first stage". Tries to show what other things the
% instrument (cutoff) is moving around:


    %%
    
    dirPlotsFS=[projectDir,'/paper/figuresCL/firstStages/'];

    if(anho==2020)
        dataRD.Properties.VariableDescriptions{'riskWhatsapp'}='Predicted placement probability at WhatsApp assigment';
dataRD.Properties.VariableDescriptions{'treatedSMSPlain'}='Received with plain SMS';
dataRD.Properties.VariableDescriptions{'treatedWarningWhatsapp'}='Read WhatsApp';
dataRD.Properties.VariableDescriptions{'treatedSMSImage'}='Received SMS w/image link';
dataRD.Properties.VariableDescriptions{'totalPopups_IniPreW'}='Pop-ups pre WhatsApp assignment';
        
        figs=cell(2,2);
                figure

        % Moving Total pous
        fs_pop=rdrobust('totalPopups','riskPopup',dataRD,'subpop',restrAll,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)');
        figs{1,1}=easyExport([dirPlotsFS,sprintf('%s_P_%s_%s',anhoStr,'fs_totalPopup','all'),'width'],'caption','First stage total pop-ups');
        
        
        figure
        % Moving SMSPlain
        fs_SP=rdrobust('treatedSMSPlain','riskPopup',dataRD,'subpop',restrAll,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)');
        figs{1,2}=easyExport([dirPlotsFS,sprintf('%s_P_%s_%s',anhoStr,'fs_SMSPlain','all'),'width'],'caption','First stage received SMS (plain, sent before whatsapp)');
        

        figure
        % Moving Whatsapp
        fs_W=rdrobust('treatedWarningWhatsapp','riskPopup',dataRD,'subpop',restrAll,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)');
        figs{2,1}=easyExport([dirPlotsFS,sprintf('%s_P_%s_%s',anhoStr,'fs_whatsapp','all'),'width'],'caption','First stage read whatsapp');
        
        
        figure
        % Moving SMSImage
        fs_SI=rdrobust('treatedSMSImage','riskPopup',dataRD,'subpop',restrAll,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)');
        figs{2,2}=easyExport([dirPlotsFS,sprintf('%s_P_%s_%s',anhoStr,'fs_SMSImage','all')],'caption','First stage received SMS (with image, after whatsapp)');
        %%
        
        colorC=[0 0.4470 0.7410];
            plotOpts_c=struct;
        plotOpts_c.color=colorC;
        plotOpts_c.shiftPointEstimate=.15;
        
        colorT=[0.6350 0.0780 0.1840];
             plotOpts_t=struct;
        plotOpts_t.color=colorT;
        plotOpts_t.shiftPointEstimate=0;
        
         figsW=cell(2,2);
        % Whatsapp first stage
        
        restrAllW=dataRD.pobWhatsapp==1&dataRD.intendedWhatsapp&dataRD.riskWhatsapp>.01&dataRD.riskWhatsapp<.99;%dataRD.mandatoryToAdd==0;
        restrAllW_c=dataRD.pobWhatsapp==1&not(dataRD.intendedWhatsapp)&dataRD.riskWhatsapp>.01&dataRD.riskWhatsapp<.99;%dataRD.mandatoryToAdd==0;
        
         figsW=cell(2,2);
                figure
        % Moving Total pous
        fs_pop_l=rdrobust('totalPopups','riskWhatsapp',dataRD,'subpop',restrAllW,'cutoff',.3,'rdrobustOpts','h(.1 .1) p(1)','withPlot',false);
        plotOpts_t.otherpointestimate=fs_pop_l;
        fs_pop=rdrobust('totalPopups','riskWhatsapp',dataRD,'subpop',restrAllW,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)','plotOpts',plotOpts_t);
        
        hold on
        fs_pop_l=rdrobust('totalPopups','riskWhatsapp',dataRD,'subpop',restrAllW_c,'cutoff',.3,'rdrobustOpts','h(.1 .1) p(1)','withPlot',false);
        plotOpts_c.otherpointestimate=fs_pop_l;
        fs_pop=rdrobust('totalPopups','riskWhatsapp',dataRD,'subpop',restrAllW_c,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)','plotOpts',plotOpts_c);
        
        hold off
        
        figsW{1,1}=easyExport([dirPlotsFS,sprintf('%s_PW_%s_%s',anhoStr,'fs_totalPopup','all'),'width'],'caption','First stage total pop-ups');
        
        
               
               
        figure
        % Moving SMSPlain
         fs_pop_l=rdrobust('treatedSMSPlain','riskWhatsapp',dataRD,'subpop',restrAllW,'cutoff',.3,'rdrobustOpts','h(.1 .1) p(1)','withPlot',false);
        plotOpts_t.otherpointestimate=fs_pop_l;
        fs_pop=rdrobust('treatedSMSPlain','riskWhatsapp',dataRD,'subpop',restrAllW,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)','plotOpts',plotOpts_t);
        
        hold on
        fs_pop_l=rdrobust('treatedSMSPlain','riskWhatsapp',dataRD,'subpop',restrAllW_c,'cutoff',.3,'rdrobustOpts','h(.1 .1) p(1)','withPlot',false);
        plotOpts_c.otherpointestimate=fs_pop_l;
        fs_pop=rdrobust('treatedSMSPlain','riskWhatsapp',dataRD,'subpop',restrAllW_c,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)','plotOpts',plotOpts_c);
        
        hold off
        
        figsW{1,2}=easyExport([dirPlotsFS,sprintf('%s_PW_%s_%s',anhoStr,'fs_SMSPlain','all'),'width'],'caption','First stage received SMS (plain, sent before whatsapp)');
        
         

        figure
        % Moving Whatsapp
        fs_pop_l=rdrobust('treatedWarningWhatsapp','riskWhatsapp',dataRD,'subpop',restrAllW,'cutoff',.3,'rdrobustOpts','h(.1 .1) p(1)','withPlot',false);
        plotOpts_t.otherpointestimate=fs_pop_l;
        fs_pop=rdrobust('treatedWarningWhatsapp','riskWhatsapp',dataRD,'subpop',restrAllW,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)','plotOpts',plotOpts_t);
        
        hold on
        fs_pop_l=rdrobust('treatedWarningWhatsapp','riskWhatsapp',dataRD,'subpop',restrAllW_c,'cutoff',.3,'rdrobustOpts','h(.1 .1) p(1)','withPlot',false);
        plotOpts_c.otherpointestimate=fs_pop_l;
        fs_pop=rdrobust('treatedWarningWhatsapp','riskWhatsapp',dataRD,'subpop',restrAllW_c,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)','plotOpts',plotOpts_c);
        ylim([-0.09,0.5])
        hold off
        figsW{2,1}=easyExport([dirPlotsFS,sprintf('%s_PW_%s_%s',anhoStr,'fs_whatsapp','all'),'width'],'caption','First stage read whatsapp');
        
        
        figure
        % Moving SMSImage
    
        

        fs_pop_l=rdrobust('treatedSMSImage','riskWhatsapp',dataRD,'subpop',restrAllW,'cutoff',.3,'rdrobustOpts','h(.1 .1) p(1)','withPlot',false);
        plotOpts_t.otherpointestimate=fs_pop_l;
        fs_pop=rdrobust('treatedSMSImage','riskWhatsapp',dataRD,'subpop',restrAllW,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)','plotOpts',plotOpts_t);
        
        hold on
        fs_pop_l=rdrobust('treatedSMSImage','riskWhatsapp',dataRD,'subpop',restrAllW_c,'cutoff',.3,'rdrobustOpts','h(.1 .1) p(1)','withPlot',false);
        plotOpts_c.otherpointestimate=fs_pop_l;
        fs_pop=rdrobust('treatedSMSImage','riskWhatsapp',dataRD,'subpop',restrAllW_c,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)','plotOpts',plotOpts_c);
        
        hold off
        figsW{2,2}=easyExport([dirPlotsFS,sprintf('%s_PW_%s_%s',anhoStr,'fs_SMSImage','all')],'caption','First stage received SMS (with image, after whatsapp)');
        
        subfiguresLatex(figsW,'file',[dirPlotsFS,sprintf('subfigs%sWhatsapp',anhoStr)],'erpf','figuresCL/firstStages/','erp','figuresCL/firstStages/','caption',['RD first stages of ',anhoStr,' WhatsApp intervention'],'label',['fig:firstsagesWRD',anhoStr])
        
        %%
        % Whatsapp balance
        dataRD.restrW=dataRD.riskWhatsapp>0.01&dataRD.riskWhatsapp<0.99&dataRD.pobWhatsapp==1&dataRD.grade<2;
        
        dataRD.riskySP=dataRD.riskSMSPlain>.3;
        dataRD.riskyP=dataRD.riskPopup>0.3;
        dataRD.totalPopupsPreWEnd=dataRD.totalPopups-dataRD.totalPopups_IniPreW;
        dataRD.totalPopupsPreWPreSI=dataRD.totalPopups_IniPreSI-dataRD.totalPopups_IniPreW;
        dataRD.schoolsAddedIniPreW=dataRD.schoolsAddedIniEnd-dataRD.schoolsAddedPreWEnd;
        % Total popups on treated RCT
        
        %%

        
        varToCheck='totalPopups_IniPreW';
        figure
        rd1=rdrobust(varToCheck,'riskWhatsapp',dataRD,'subpop',dataRD.restrW&dataRD.intendedWhatsapp==1,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)');
        title('')
       
        easyExport([dirPlotsFS,sprintf('%s_PW_%s_%s',anhoStr,'fsPopUps_WhatsappTreated','all')]);
        
        % Total popups on non-treated RCT
        hold on
        plotOpts_c=struct;plotOpts_c.color=linspecer(1);plotOpts_c.shiftPointEstimate=.15;
        rd2=rdrobust(varToCheck,'riskWhatsapp',dataRD,'subpop',dataRD.restrW&dataRD.controlWhatsapp==1,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)','plotOpts',plotOpts_c);
        title('')
        %easyExport([dirPlotsFS,sprintf('%s_P_%s_%s',anhoStr,'fsPopUps_WhatsappControl','all')]);
        
        legend('Location','SouthEast');  % this sequence shows just the first 2 legends
        legend([rd1.functionLine rd2.functionLine],{'With whatsapp' 'Without whatsapp'},'Interpreter','latex');
        legend boxoff
        
        stataExpress({sprintf('reg %s intendedWhatsapp  if restrW==1',varToCheck),...
            sprintf('reg %s intendedWhatsapp  if restrW==1&riskWhatsapp<.3',varToCheck),...
            sprintf('reg %s intendedWhatsapp  if restrW==1&riskWhatsapp>.3',varToCheck)},dataRD);


        
        
    elseif(anho==2019)
        
         figs=cell(1,1);
                figure
                
        % Moving Total pous
        fs_pop=rdrobust('totalPopups','riskPopup',dataRD,'subpop',restrAll,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)');
        figs{1,1}=easyExport([dirPlotsFS,sprintf('%s_P_%s_%s',anhoStr,'fs_totalPopup','all'),'width'],'caption','First stage total pop-ups');
        
        
        % No hay interaccion, SMS1 y SMS2 son para gente que no recibió popup
        

    elseif(anho==2018)
        
          dataRD.Properties.VariableDescriptions{'treatedSMSB'}='Received reminder SMS';
          
        figs=cell(1,2);
                figure
        % Moving Total pous
        fs_pop=rdrobust('totalPopups','riskPopup',dataRD,'subpop',restrAll,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)');
        figs{1,1}=easyExport([dirPlotsFS,sprintf('%s_P_%s_%s',anhoStr,'fs_totalPopup','all'),'width'],'caption','First stage total pop-ups');
        
        
        
        % Hay un SMS para pobalcion sin API
        % SMS de recordatorio
        figure
        fs_W=rdrobust('treatedSMSB','riskPopup',dataRD,'subpop',restrAll,'cutoff',.3,'rdrobustOpts','h(.28 .68) p(2)');
           figs{1,2}=easyExport([dirPlotsFS,sprintf('%s_P_%s_%s',anhoStr,'fs_SMSReminder','all'),'width'],'caption','First stage reminder SMS');
        
    end

end
%subfiguresLatex(figs,'file',[dirPlotsFS,sprintf('subfigs%s',anhoStr)],'erpf','figuresCL/firstStages/','erp','figuresCL/firstStages/','caption',['RD first stages of ',anhoStr,' popup'],'label',['fig:firstsagesRD',anhoStr])